<?php

namespace app\api\service;

use app\api\model\ArticleCategory;
use app\api\model\ArticleList;

class ArticleService
{
    public static function getCategoryList($params)
    {
        $list = ArticleCategory::getList();
        $data = array_map(function ($item) {
            return [
                'category_id' => $item['category_id'],
                'category_title' => $item['category_title'],
            ];
        }, $list);
        return [200, '操作成功', $data];
    }

    public static function getArticleList($params)
    {
        $where = [];
        $where[] = ['article_status', '=', 1];
        $where[] = ['category_id', '=', $params['category_id']];
        if ($params['sort_type']) $order = 'article_id desc';
        else $order = 'article_views desc,article_id desc';
        $list = ArticleList::getListByWhere($where, $order, $params['page_no'], $params['page_size']);
        $total = ArticleList::countListByWhere($where);
        $data = array_map(function ($item) {
            return [
                'article_id' => $item['article_id'],
                'article_title' => $item['article_title'],
                'article_describe' => $item['article_describe'],
                'article_thumb' => dealImage($item['article_thumb']),
                'article_views' => dealNumUnit($item['article_views']),
                'article_likes' => dealNumUnit($item['article_likes']),
            ];
        }, $list);
        return [200, '操作成功', ['total' => $total, 'list' => $data]];
    }

    public static function getArticleInfo($params)
    {
        $info = ArticleList::getArticleInfo($params['article_id']);
        if (!$info) return [200, '文章内容不存在', []];
        $info->save(['article_views' => $info['article_views'] + 1]);
        $data['article_id'] = $info['article_id'];
        $data['article_title'] = $info['article_title'];
        $data['article_content'] = $info['article_content'];
        $data['article_views'] = dealNumUnit($info['article_views']);
        $data['article_likes'] = dealNumUnit($info['article_likes']);
        return [200, '操作成功', $data];
    }
}